package com.dayaokeji.rhythmschoolstudent.client.mine.account;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.mine.account.adapter.SchoolListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.SchoolApi;
import com.dayaokeji.server_api.domain.School;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 搜索学校
 */
public class SearchSchoolActivity extends BaseActivity {

    public static final String EXTRA_SCHOOL_ENTITY = "school";

    private static final SchoolApi schoolApi = ApiUtils.getApi(SchoolApi.class);

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_school_list)
    RecyclerView rvSchoolList;

    private List<School> schools = new ArrayList<>();
    private Call<ServerResponse<List<School>>> listCall;
    private SchoolListAdapter schoolListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_school);
        setSupportActionBar(toolbar);

        init();
    }


    private void init() {
        setRecyclerView();
        setOnListener();
        getSchoolData("");
    }

    private void setRecyclerView() {
        rvSchoolList.setLayoutManager(new LinearLayoutManager(this));
        rvSchoolList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .marginResId(R.dimen.default_divider_height)
                .build());
        schoolListAdapter = new SchoolListAdapter(schools);
        rvSchoolList.setAdapter(schoolListAdapter);
        schoolListAdapter.setEmptyView(R.layout.empty_layout,rvSchoolList);
    }

    private void setOnListener() {
        schoolListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                School school = (School) adapter.getData().get(position);
                Intent intent = getIntent();
                intent.putExtra(EXTRA_SCHOOL_ENTITY,school);
                setResult(RESULT_OK,intent);
                ActivityStackManager.finish();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_school, menu);

        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search_school).getActionView();
        searchView.setIconified(false);
        searchView.setIconifiedByDefault(false);
        searchView.onActionViewExpanded();
        searchView.setMaxWidth(1000);
        searchView.setQueryHint(getString(R.string.search_school));
        searchView.setOnQueryTextListener(queryTextListener);

        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 搜索栏文字变化监听
     */
    private final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            getSchoolData(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            getSchoolData(newText);
            return false;
        }
    };

    /**
     * 查询学校
     * @param name
     */
    private void getSchoolData(String name) {
        listCall = schoolApi.list(1, name);
        listCall.enqueue(new ServerCallback<List<School>>(this,"加载中...") {
            @Override
            public void onResponse(boolean success, ServerResponse<List<School>> response) {
                if (success) {
                    schools.clear();
                    schools.addAll(response.getBody());
                    schoolListAdapter.notifyDataSetChanged();
                }else{
                    ToastUtils.error("没有查询到结果");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (listCall != null) {
            listCall.cancel();
        }
    }
}
